package com.thinvent.remotepassword.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.thinvent.remotepassword.entity.User;
import com.thinvent.remotepassword.service.UserService;
import com.thinvent.remotepassword.util.JwtUtil;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("login")
@CrossOrigin(origins = "*")
public class LoginController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;
    /**
     * 查询登录数据
     ** @param user 查询实体
     * @return 登录数据
     */
    @GetMapping("/login")
    public R login(User user) {
        User one = this.userService.getOne(new QueryWrapper<>(user));
        if(one==null ||one.getId() == null){
            return failed("登录失败！账户或者密码错误");
        }
        String token = JwtUtil.getToken(one);
        Map<String, Object> map = new HashMap<>();
        map.put("token",token);
        map.put("user",one);
        return success(map);
    }
}
